home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4490 < prev    next >
Encoding:
Text File  |  1996-08-05  |  964 b   |  44 lines

  1. Path: sn.no!usenet
  2. From: jan-henrik.haukeland@fou.telenor.no (jan-henrik haukeland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to tell if a file exists in C
  5. Date: 05 Feb 1996 00:31:06 +0100
  6. Organization: LH*281263
  7. Message-ID: <m2pwbuvfb9.fsf@hawk.no>
  8. References: <4eqkj6$ipo@charm.magnus.acs.ohio-state.edu>
  9. NNTP-Posting-Host: nm6-ppp4.oslo.net
  10. In-reply-to: xiaoyi@bmecg.bme.ohio-state.edu's message of 1 Feb 1996 15:00:54
  11.     GMT
  12. To: xiaoyi@mozart.bme.ohio-state.edu (Xiaoyi Wu)
  13. X-Newsreader: Gnus v5.0.12
  14.  
  15. Xiaoyi Wu writes:
  16.  
  17.  : Hi, how do I find out if a file already exists
  18.  : in UNIX C? On PCs I would do a findfirst/findnext,
  19.  : is there an equivalent on Unix?
  20.  
  21. Use the stat function, e.g.:
  22.  
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25.  
  26. /* Check if file exists */
  27. int fexist(char * filename)
  28. {
  29.   struct stat buf;
  30.  
  31.   if (( stat (filename, &buf)) < 0)
  32.     return (FALSE);
  33.  
  34.   if (! S_ISREG(buf.st_mode)) {
  35.     return (NOT_A_REGULAR_FILE);
  36.   }
  37.  
  38.   return(TRUE);
  39. }
  40.  
  41.  
  42.  
  43. jhh
  44.